package com.shengqi.xinnongren.work.ui.home.presenter;

import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.ui.home.view.NewsListView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class NewsListPresenter extends BasePresenter<NewsListView> {
    public void getNewsList(int pageIndex) {
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageIndex);
        map.put("size", 50);
        JSONObject jsonData = Constants.getJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());

        addTask(RetrofitHelper.getInstance().getService().getNewsList(requestBody), listHttpRespond -> {
            if (listHttpRespond.result == 1) {
                getView().getNewsListRes(listHttpRespond);
            } else
                getView().onRequestFailed(listHttpRespond.message);
        });
    }

    public void getNewsDetails(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        JSONObject jsonData = Constants.getJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getNewsDetails(requestBody), respond -> {
            if (respond.result == 1) {
                getView().showNewsDetails(respond.data);
            } else
                getView().showRequestFailed(respond.message);
        });
    }
}
